//
//  AccountProductDetailViewController.m
//  NOAHWM
//
//  Created by Ryan on 13-6-22.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "AccountProductDetailViewController.h"
#import "NYMemberCenterDataManager.h"
#import "DynamicDetailViewController.h"

@interface AccountProductDetailViewController ()

@end

@implementation AccountProductDetailViewController

@synthesize accountProduct,accountProductDetail;
@synthesize detailView,dynamicView,tradeDetailView;


- (AccountProductDetailView *)detailView
{
    if(nil == detailView)
    {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"AccountProductDetailView" owner:self options:nil];
        detailView = [[nibs lastObject] retain];
    }
    return detailView;
}

- (AccountDynamicView *)dynamicView
{
    if(nil == dynamicView)
    {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"AccountDynamicView" owner:self options:nil];
        dynamicView = [[nibs lastObject] retain];
    }
    return dynamicView;
}

- (AccountTradeDetailView *)tradeDetailView
{
    if(nil == tradeDetailView)
    {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"AccountTradeDetailView" owner:self options:nil];
        tradeDetailView = [[nibs lastObject] retain];
    }
    return tradeDetailView;
}

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightItemTapped
{
    NSArray *properties = [NSArray arrayWithObject:@"Call"];
    NSMutableDictionary *filterInfo = [NSMutableDictionary dictionary];
    [filterInfo setObject:properties forKey:kFilterPropertiesKey];
    [filterInfo setObject:[NSNumber numberWithInt:kLocationRight] forKey:kFilterLocationType];
    [filterInfo setObject:[NSNumber numberWithInteger:kAccountProductDetailFilter] forKey:kFilterTypeKey];
    [[NSNotificationCenter defaultCenter] postNotificationName:kShowFilterViewNotification object:nil userInfo:filterInfo];
}

#pragma mark - Notification methods
- (void)showDynamicListFullScreenWithNotification:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3f animations:^(void){
        CGRect detailViewRect = self.detailView.frame;
        CGRect tradeViewRect = self.tradeDetailView.frame;
        CGRect listViewRect = self.dynamicView.frame;
        detailViewRect.origin.y = kNaviHeaderHeight-kAccountProductDetailViewHeight;
        tradeViewRect.origin.y = self.view.frame.size.height;
        listViewRect.origin.y = kNaviHeaderHeight;
        listViewRect.size.height = self.view.frame.size.height-kNaviHeaderHeight;
        self.detailView.frame = detailViewRect;
        self.tradeDetailView.frame = tradeViewRect;
        self.dynamicView.frame = listViewRect;
        self.detailView.alpha = 0;
        self.tradeDetailView.alpha = 0;
    }];
    NSLog(@"full...");
}

- (void)showDynamicListHalfScreenWithNotification:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3f animations:^(void){
        CGRect detailViewRect = self.detailView.frame;
        CGRect tradeViewRect = self.tradeDetailView.frame;
        CGRect listViewRect = self.dynamicView.frame;
        detailViewRect.origin.y = kNaviHeaderHeight;
        tradeViewRect.origin.y = self.view.frame.size.height-kNaviHeaderHeight;
        listViewRect.origin.y = kNaviHeaderHeight+kAccountProductDetailViewHeight;
        listViewRect.size.height = self.view.frame.size.height-kNaviHeaderHeight-kAccountProductDetailViewHeight-kNaviHeaderHeight;
        self.detailView.frame = detailViewRect;
        self.tradeDetailView.frame = tradeViewRect;
        self.dynamicView.frame = listViewRect;
        self.detailView.alpha = 1.f;
        self.tradeDetailView.alpha = 1.f;
    }];
    NSLog(@"half...");
}

- (void)showDynamicDetailViewWithNotification:(NSNotification *)notification
{
    DynamicDetailViewController *ddvc = [[DynamicDetailViewController alloc] init];
    ddvc.accountDynamic = notification.object;
    ddvc.isRightMenuEnter = YES;
    [self.navigationController pushViewController:ddvc animated:YES];
    [ddvc release];
}
#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle:self.accountProduct.product_name];
    [self setLeftNaviItemWithTitle:nil imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:nil imageName:@"Icon_Dial"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDynamicListFullScreenWithNotification:) name:kShowAccountListFullScreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDynamicListHalfScreenWithNotification:) name:kShowAccountListHalfScreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDynamicDetailViewWithNotification:) name:kShowDynamicDetailViewNotification object:nil];
    [self.view addSubview:self.detailView];
    CGRect rect = self.detailView.frame;
    rect.origin.y = kNaviHeaderHeight;
    self.detailView.frame = rect;
    accountProductDetail = [[NYAccountProductDetail alloc] init];
    self.detailView.accountProductDetail = self.accountProductDetail;
    [self.detailView callServerToGetAccountProductDetailWithAccountId:[NYMemberCenterDataManager sharedManager].loginMember.accountId productId:self.accountProduct.product_id];

    CGRect dynamicViewRect = self.dynamicView.frame;
    dynamicViewRect.origin.y = kNaviHeaderHeight+self.detailView.frame.size.height;
    self.dynamicView.frame = dynamicViewRect;
    [self.view addSubview:self.dynamicView];
    
    [self.dynamicView callServerToGetDynamicList:[NYMemberCenterDataManager sharedManager].loginMember.accountId productId:self.accountProduct.product_id];
    if(Is4InchSizeScreen)
    {
        self.tradeDetailView.frame = CGRectMake(0, self.view.frame.size.height-kNaviHeaderHeight, self.view.frame.size.width, self.view.frame.size.height-kNaviHeaderHeight);
    }
    else
    {
        self.tradeDetailView.frame = CGRectMake(0, k3InchSizheAppHeight-kNaviHeaderHeight, self.view.frame.size.width, k3InchSizheAppHeight-kNaviHeaderHeight);
    }
    [self.view addSubview:self.tradeDetailView];
    [self.tradeDetailView callServerToGetTradeDetailWithAccountId:[NYMemberCenterDataManager sharedManager].loginMember.accountId productId:self.accountProduct.product_id];
    
    [self.view bringSubviewToFront:self.headerView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        self.view = nil;
        self.detailView = nil;
        self.dynamicView = nil;
        self.tradeDetailView = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.detailView = nil;
    self.dynamicView = nil;
    self.tradeDetailView = nil;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [tradeDetailView release];
    [dynamicView release];
    [accountProductDetail release];
    [detailView release];
    [accountProduct release];
    [super dealloc];
}
@end
